
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>全景图传输 - 浪子之心</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Blog,"> 
    <meta name="description" content="MrChen Bolg,全景图知识储备将全景图转变为Byte数组12345Camera.main.RenderToCubemap(cm);texture2D = new Texture2D(width, hight);te,"> 
    <meta name="author" content="Jack Chen"> 
    <link rel="alternative" href="atom.xml" title="浪子之心" type="application/atom+xml"> 
    <link rel="icon" href="/blog/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/blog/css/diaspora.css">

<meta name="generator" content="Hexo 5.4.1"><link rel="stylesheet" href="/blog/css/prism.css" type="text/css"></head>

<body class="loading">
    <span id="config-title" style="display:none">浪子之心</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://mrchenlearnspace.github.io/blog"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">全景图传输</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">全景图传输</h1>
        <div class="stuff">
            <span>三月 21, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/blog/tags/%E5%85%A8%E6%99%AF%E5%9B%BE/" rel="tag">全景图</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/blog/tags/%E7%BD%91%E7%BB%9C/" rel="tag">网络</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="全景图"><a href="#全景图" class="headerlink" title="全景图"></a>全景图</h1><h2 id="知识储备"><a href="#知识储备" class="headerlink" title="知识储备"></a>知识储备</h2><h3 id="将全景图转变为Byte数组"><a href="#将全景图转变为Byte数组" class="headerlink" title="将全景图转变为Byte数组"></a>将全景图转变为Byte数组</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Camera.main.RenderToCubemap(cm);</span><br><span class="line">texture2D = <span class="keyword">new</span> Texture2D(width, hight);</span><br><span class="line">texture2D.SetPixels(cm.GetPixels(CubemapFace.NegativeZ));</span><br><span class="line">texture2D.Apply();</span><br><span class="line">byt = texture2D.EncodeToPNG();</span><br></pre></td></tr></table></figure>

<h3 id="将Byte数组变成全景图"><a href="#将Byte数组变成全景图" class="headerlink" title="将Byte数组变成全景图"></a>将Byte数组变成全景图</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">texture2DFix.LoadImage(byt);</span><br><span class="line">texture2DFix.Apply</span><br><span class="line">cm1.SetPixels(texture2DFix.GetPixels(), CubemapFace.PositiveZ);</span><br><span class="line">cm1.Apply();</span><br></pre></td></tr></table></figure>

<h3 id="网络传输"><a href="#网络传输" class="headerlink" title="网络传输"></a>网络传输</h3><p>名字设为 I P 地址名</p>
<p>1.发送连接信号并在服务器创建位置</p>
<h3 id="图像处理"><a href="#图像处理" class="headerlink" title="图像处理"></a>图像处理</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">1</span>; i &lt; width + <span class="number">1</span>; i++) Array.Copy(cmmcolors, width * (width - i), ReCmmcolors, width * (i - <span class="number">1</span>), width);<span class="comment">//上下翻转</span></span><br></pre></td></tr></table></figure>

<h3 id="协议处理"><a href="#协议处理" class="headerlink" title="协议处理"></a>协议处理</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">print(point.Right.Length);</span><br><span class="line">      <span class="built_in">string</span> str = pos.x.ToString() + <span class="string">&quot; &quot;</span> + pos.y.ToString() + <span class="string">&quot; &quot;</span> + pos.z.ToString() + <span class="string">&quot; &quot;</span> + <span class="string">&quot;PX &quot;</span>;</span><br><span class="line">      <span class="built_in">byte</span>[] bytstr= System.Text.Encoding.UTF8.GetBytes(str);<span class="comment">//单独编译</span></span><br><span class="line"></span><br><span class="line">      <span class="built_in">byte</span>[] sendByt = <span class="keyword">new</span> <span class="built_in">byte</span>[point.Right.Length+ bytstr.Length];</span><br><span class="line">      bytstr.CopyTo(sendByt, <span class="number">0</span>);</span><br><span class="line">      point.Right.CopyTo(sendByt, bytstr.Length);<span class="comment">//合并编码</span></span><br></pre></td></tr></table></figure>

<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"> Texture2D = <span class="keyword">new</span> Texture2D(width, width);</span><br><span class="line"> <span class="built_in">byte</span>[] image = sendByt.Skip(bytstr.Length).ToArray();<span class="comment">//需要using System.Linq;//截取字节</span></span><br><span class="line"> print(image.Length);</span><br><span class="line"><span class="comment">// Array.Reverse(image);//可以使图像旋转180度</span></span><br><span class="line"> print(Texture2D.LoadImage(image));</span><br><span class="line"> Texture2D.Apply();<span class="number">5.4</span><span class="number">.1</span></span><br></pre></td></tr></table></figure>

<h3 id="获取本地-I-P-地址"><a href="#获取本地-I-P-地址" class="headerlink" title="获取本地 I P 地址"></a>获取本地 I P 地址</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">GetLocalIp</span>()</span></span><br><span class="line">   &#123;</span><br><span class="line">       <span class="comment"><span class="doctag">///</span>获取本地的IP地址</span></span><br><span class="line">       <span class="built_in">string</span> AddressIP = <span class="built_in">string</span>.Empty;</span><br><span class="line">       <span class="keyword">foreach</span> (IPAddress _IPAddress <span class="keyword">in</span> Dns.GetHostEntry(Dns.GetHostName()).AddressList)</span><br><span class="line">       &#123;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">if</span> (_IPAddress.AddressFamily.ToString() == <span class="string">&quot;InterNetwork&quot;</span>)</span><br><span class="line">           &#123;</span><br><span class="line"></span><br><span class="line">               AddressIP = _IPAddress.ToString();</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> AddressIP;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h2 id="基本网络架构"><a href="#基本网络架构" class="headerlink" title="基本网络架构"></a>基本网络架构</h2><h2 id="服务器端"><a href="#服务器端" class="headerlink" title="服务器端"></a>服务器端</h2><p><img src="https://pica.zhimg.com/80/v2-e5925782976fc81a36cca0f889cec93a_720w.png"></p>
<h3 id="Connection"><a href="#Connection" class="headerlink" title="Connection"></a>Connection</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">CloudService</span>()</span> &#123;</span><br><span class="line">       msgHandle = <span class="keyword">new</span> MsgHandle();</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="built_in">int</span> <span class="title">NewIndex</span>()</span> &#123;</span><br><span class="line">       <span class="keyword">if</span> (conns == <span class="literal">null</span>) &#123;</span><br><span class="line">           <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; conns.Length; i++) &#123;</span><br><span class="line">           <span class="keyword">if</span> (conns[i] == <span class="literal">null</span>) &#123;</span><br><span class="line">               conns[i] = <span class="keyword">new</span> Conn();</span><br><span class="line">               <span class="keyword">return</span> i;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">else</span> <span class="keyword">if</span> (conns[i].isUse == <span class="literal">false</span>) &#123;</span><br><span class="line">               <span class="keyword">return</span> i;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ServiceStart</span>(<span class="params"><span class="built_in">string</span> host, <span class="built_in">int</span> port</span>)</span> &#123;</span><br><span class="line">       conns = <span class="keyword">new</span> Conn[maxConn];</span><br><span class="line">       <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; maxConn; i++) &#123;</span><br><span class="line">           conns[i] = <span class="keyword">new</span> Conn();</span><br><span class="line">       &#125;</span><br><span class="line">       Listerfd = <span class="keyword">new</span> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);</span><br><span class="line">       IPAddress ipAdr = IPAddress.Parse(host);</span><br><span class="line">       IPEndPoint ipEp = <span class="keyword">new</span> IPEndPoint(ipAdr, port);</span><br><span class="line">       Listerfd.Bind(ipEp);</span><br><span class="line">       Listerfd.Listen(maxConn);</span><br><span class="line">       Listerfd.BeginAccept(AcceptCb, <span class="literal">null</span>);</span><br><span class="line">       status = <span class="literal">true</span>;</span><br><span class="line">       NetCloudServerManager.instance.debug.text+=(<span class="string">&quot;\n&quot;</span>+<span class="string">&quot;[服务器]启动成功&quot;</span>);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h3 id="Accept-Cb-异步监听"><a href="#Accept-Cb-异步监听" class="headerlink" title="Accept Cb 异步监听"></a>Accept Cb 异步监听</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">AcceptCb</span>(<span class="params">IAsyncResult ar</span>)</span> &#123;</span><br><span class="line">        Socket socket = Listerfd.EndAccept(ar);<span class="comment">//要放在try外面</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//接收客户端</span></span><br><span class="line">            <span class="built_in">int</span> index = NewIndex();</span><br><span class="line">            <span class="keyword">if</span> (index &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                socket.Close();</span><br><span class="line">                Debug.Log(<span class="string">&quot;[警告]连接已满&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                Conn conn = conns[index];</span><br><span class="line">                conn.Init(socket);</span><br><span class="line">                <span class="built_in">string</span> host = conn.GetAdress();</span><br><span class="line">                Debug.Log(<span class="string">&quot;客户端连接:[&quot;</span> + host + <span class="string">&quot;] conn池ID:&quot;</span> + index);</span><br><span class="line">                conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReciveCb, conn);<span class="comment">//接收的同时调用ReciveCb回调函数</span></span><br><span class="line">            &#125;</span><br><span class="line">            Listerfd.BeginAccept(AcceptCb, <span class="literal">null</span>);<span class="comment">//再次调用AcceprCb回调函数</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            Debug.Log(<span class="string">&quot;AccpetCb 失败:&quot;</span> + e.Message);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>出现莫名其妙的问题，等待异步需要放在 try 外面，不然会抛出 AcceptCb只能在主线程调用，要我放在awake和start中</p>
<h3 id="Recive-Cb异步接收"><a href="#Recive-Cb异步接收" class="headerlink" title="Recive Cb异步接收"></a>Recive Cb异步接收</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ReciveCb</span>(<span class="params">IAsyncResult ar</span>)</span> &#123;</span><br><span class="line">        Conn conn = (Conn)ar.AsyncState;<span class="comment">//这个AsyncState就是上面那个BeginRecive函数里面最后一个参数</span></span><br><span class="line">        <span class="keyword">lock</span> (conn) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="built_in">int</span> count = conn.socket.EndReceive(ar);<span class="comment">//返回接收的字节数</span></span><br><span class="line">                                                       <span class="comment">//没有信息就关闭</span></span><br><span class="line">                <span class="keyword">if</span> (count &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">                    Debug.Log(<span class="string">&quot;收到[&quot;</span> + conn.GetAdress() + <span class="string">&quot;] 断开连接&quot;</span>);</span><br><span class="line">                    conn.Close();</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                conn.buffCount += count;</span><br><span class="line">                ProcessData(conn);</span><br><span class="line">               </span><br><span class="line">                <span class="comment">//继续接收</span></span><br><span class="line">                conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReciveCb, conn);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                Debug.Log(<span class="string">&quot;Recive失败&quot;</span> + e.Message);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ProcessData</span>(<span class="params">Conn conn</span>)</span> &#123;</span><br><span class="line">        <span class="comment">//小于字节长度</span></span><br><span class="line">        <span class="keyword">if</span> (conn.buffCount &lt; <span class="keyword">sizeof</span>(Int32)) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Debug.Log(<span class="string">&quot;接收到了 &quot;</span> + conn.buffCount + <span class="string">&quot; 个字节&quot;</span>);</span><br><span class="line">        Array.Copy(conn.readBuff, conn.lenByte, <span class="keyword">sizeof</span>(Int32));</span><br><span class="line">        conn.msgLength = BitConverter.ToInt32(conn.lenByte, <span class="number">0</span>);</span><br><span class="line">        <span class="comment">//小于最小要求长度则返回表示未接收完全</span></span><br><span class="line">        <span class="keyword">if</span> (conn.buffCount &lt; conn.msgLength + <span class="keyword">sizeof</span>(Int32)) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">        <span class="comment">//这里接收信息有个细节，因为之前发送回来的信息又被加了一次长度，相当于要把他所有的信息接收完了</span></span><br><span class="line">        <span class="comment">//才算接收成功，然后再把前面的sizeof(Int32)去掉，剩下的就是带长度的信息了</span></span><br><span class="line">        ProtocolByte proto = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        ProtocolByte protoStr = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        ProtocolByte protocol = proto.Decode(conn.readBuff, <span class="keyword">sizeof</span>(Int32), conn.msgLength) <span class="keyword">as</span> ProtocolByte;</span><br><span class="line">        protoStr.AddString(conn.GetAdress());</span><br><span class="line">        protocol.bytes = protoStr.bytes.Concat(protocol.bytes).ToArray();</span><br><span class="line">        <span class="keyword">lock</span>(msgHandle.msgList) &#123;</span><br><span class="line">            msgHandle.msgList.Add(protocol);</span><br><span class="line">        &#125;</span><br><span class="line">        Debug.Log( protocol.GetDesc());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">//清除已处理的消息</span></span><br><span class="line">        <span class="built_in">int</span> count = conn.buffCount - conn.msgLength - <span class="keyword">sizeof</span>(Int32);</span><br><span class="line">        Array.Copy(conn.readBuff, <span class="keyword">sizeof</span>(Int32) + conn.msgLength, conn.readBuff, <span class="number">0</span>, count);</span><br><span class="line">        conn.buffCount = count;</span><br><span class="line">        <span class="comment">//如果还有多余信息就继续处理</span></span><br><span class="line">        <span class="keyword">if</span> (conn.buffCount &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            ProcessData(conn);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<h3 id="Conn类"><a href="#Conn类" class="headerlink" title="Conn类"></a>Conn类</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Conn</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">const</span> <span class="built_in">int</span> BUFFER_SIZE = <span class="number">16384</span>;</span><br><span class="line">    <span class="keyword">public</span> Socket socket;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">bool</span> isUse = <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">byte</span>[] readBuff = <span class="keyword">new</span> <span class="built_in">byte</span>[BUFFER_SIZE];</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> buffCount = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">public</span> Int32 msgLength = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">byte</span>[] lenByte = <span class="keyword">new</span> <span class="built_in">byte</span>[<span class="keyword">sizeof</span>(Int32)];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> ProtocolByte assistProtolByte;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Conn</span>()</span> &#123;</span><br><span class="line">        readBuff = <span class="keyword">new</span> <span class="built_in">byte</span>[BUFFER_SIZE];</span><br><span class="line">        assistProtolByte = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Init</span>(<span class="params">Socket socket</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">this</span>.socket = socket;</span><br><span class="line">        isUse = <span class="literal">true</span>;</span><br><span class="line">        buffCount = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">int</span> <span class="title">BuffRemain</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> BUFFER_SIZE - buffCount;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">GetAdress</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!isUse) &#123; <span class="keyword">return</span> <span class="string">&quot;无法获取地址&quot;</span>; &#125;</span><br><span class="line">        <span class="keyword">return</span> socket.RemoteEndPoint.ToString();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Close</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!isUse) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Console.WriteLine(<span class="string">&quot;[断开连接]&quot;</span> + GetAdress());</span><br><span class="line">        socket.Close();</span><br><span class="line">        isUse = <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">bool</span> <span class="title">SendMsg</span>(<span class="params">ProtocolBase protol</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">string</span> proName = protol.GetName(); ;</span><br><span class="line">        <span class="keyword">return</span> SendMsg(protol, proName);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">bool</span> <span class="title">SendMsg</span>(<span class="params">ProtocolBase protol, <span class="built_in">string</span> protolName</span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">        ProtocolByte protocolByte1 = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        protocolByte1.AddString(protolName);</span><br><span class="line">        <span class="built_in">byte</span>[] protolNameByte = protocolByte1.bytes;</span><br><span class="line">        <span class="built_in">byte</span>[] b = protol.Encode();</span><br><span class="line">        <span class="built_in">byte</span>[] len1 = BitConverter.GetBytes(protolNameByte.Length + b.Length);</span><br><span class="line">        <span class="built_in">byte</span>[] sendByte = len1.Concat(protolNameByte).Concat(b).ToArray();</span><br><span class="line">        socket.Send(sendByte);</span><br><span class="line">        Debug.Log(<span class="string">&quot;sendByte &quot;</span> + sendByte.Length);</span><br><span class="line">        Debug.Log(<span class="string">&quot;sendBytes &quot;</span> + GetDesc(sendByte));</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span>  <span class="built_in">string</span> <span class="title">GetDesc</span>(<span class="params"><span class="built_in">byte</span>[] bytes</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">string</span> str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>) <span class="keyword">return</span> str;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; bytes.Length; i++) &#123;</span><br><span class="line">            <span class="built_in">int</span> b = (<span class="built_in">int</span>)bytes[i];</span><br><span class="line">            str += b.ToString() + <span class="string">&quot; &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="消息处理类"><a href="#消息处理类" class="headerlink" title="消息处理类"></a>消息处理类</h3><p>区别于客户端消息类，处理消息时，需要在接收的消息中加入传来的消息IP地址防止找不到回溯玩家。</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">MsgHandle</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> num = NetCloudServerManager.instance.HandleMsgNum;</span><br><span class="line">    Dictionary&lt;<span class="built_in">string</span>, ProtocolBase&gt; oneMessager = <span class="keyword">new</span> Dictionary&lt;<span class="built_in">string</span>, ProtocolBase&gt;();</span><br><span class="line">    <span class="keyword">public</span> List&lt;ProtocolBase&gt; msgList = <span class="keyword">new</span> List&lt;ProtocolBase&gt;();</span><br><span class="line"></span><br><span class="line">    ProtocolByte assistProtocol;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Running</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">while</span>(msgList.Count&gt;<span class="number">0</span>&amp;&amp;num&gt;<span class="number">0</span>) &#123;</span><br><span class="line">            num--;</span><br><span class="line">            <span class="comment">//处理长度和将协议分开</span></span><br><span class="line">            assistProtocol = msgList[<span class="number">0</span>] <span class="keyword">as</span> ProtocolByte;</span><br><span class="line">            <span class="keyword">if</span>(assistProtocol==<span class="literal">null</span>) &#123;</span><br><span class="line">                Debug.Log(<span class="string">&quot;消息转化失败&quot;</span>);</span><br><span class="line">                <span class="keyword">lock</span> (msgList) &#123;</span><br><span class="line">                    msgList.RemoveAt(<span class="number">0</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">int</span> start = <span class="number">0</span>;</span><br><span class="line">            <span class="built_in">string</span> IpAddress = assistProtocol.GetString(start, <span class="keyword">ref</span> start);</span><br><span class="line">            <span class="built_in">string</span> protocolName=assistProtocol.GetString(start, <span class="keyword">ref</span> start);</span><br><span class="line">            <span class="comment">//int protocolNameLen=System.Text.Encoding.UTF8.GetBytes(protocolName).Length + sizeof(Int32);</span></span><br><span class="line">            <span class="keyword">switch</span> (protocolName) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;ProtocolCubeTexture&quot;</span>:</span><br><span class="line">                    ProtocolCubeTexture protocolCubeTexture = <span class="keyword">new</span> ProtocolCubeTexture();</span><br><span class="line">                    ProtocolCubeTexture aprotocol = protocolCubeTexture.Decode(assistProtocol.bytes, start, assistProtocol.bytes.Length) <span class="keyword">as</span> ProtocolCubeTexture;</span><br><span class="line">                    <span class="keyword">if</span> (aprotocol == <span class="literal">null</span>) <span class="keyword">break</span>;</span><br><span class="line">                    PointMessage point = <span class="keyword">new</span> PointMessage();</span><br><span class="line">                    point.ResolveToPointMessage(aprotocol.Coord, aprotocol.CubemapFace, aprotocol.srcTexture);</span><br><span class="line">                    NetCloudServerManager.instance.PM.Add(point);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;Start&quot;</span>:</span><br><span class="line">                    StartProcessData(assistProtocol, start, IpAddress);</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">lock</span>(msgList) &#123;</span><br><span class="line">                msgList.RemoveAt(<span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//ProtocolCubeTexture protocol = msgList[0] as ProtocolCubeTexture;</span></span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        num = NetCloudServerManager.instance.HandleMsgNum;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">StartProcessData</span>(<span class="params">ProtocolByte protocol,<span class="built_in">int</span> start,<span class="built_in">string</span> SendIPAddress</span>)</span> &#123;</span><br><span class="line">        ProtocolStr protocolStr = <span class="keyword">new</span> ProtocolStr();</span><br><span class="line">        Debug.Log(<span class="string">&quot;start&quot;</span> + start + <span class="string">&quot; lenth&quot;</span> + assistProtocol.bytes.Length);</span><br><span class="line">        ProtocolStr protocolstr = protocolStr.Decode(assistProtocol.bytes, start, assistProtocol.bytes.Length-start) <span class="keyword">as</span> ProtocolStr;</span><br><span class="line">        NetCloudServerManager.instance.debug.text += (<span class="string">&quot;\n&quot;</span> + protocolstr.str);</span><br><span class="line">        <span class="built_in">string</span>[] CoordStrs = protocolstr.str.Split(<span class="string">&#x27;,&#x27;</span>);</span><br><span class="line">        <span class="built_in">float</span>[] Coord = <span class="keyword">new</span> <span class="built_in">float</span>[<span class="number">3</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++)</span><br><span class="line">            Coord[i] = <span class="built_in">float</span>.Parse(CoordStrs[i]);</span><br><span class="line">        Camera camm = NetCloudServerManager.instance.cam;</span><br><span class="line">        camm.transform.position = <span class="keyword">new</span> Vector3( Coord[<span class="number">0</span>], Coord[<span class="number">1</span>], Coord[<span class="number">2</span>]);</span><br><span class="line">        PointMessage point = <span class="keyword">new</span> PointMessage();</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> ( camm.RenderToCubemap(point.cubemap)) &#123;</span><br><span class="line">            <span class="built_in">int</span> index = <span class="number">-1</span>;</span><br><span class="line">            Conn[] conns = NetCloudServerManager.cs.conns;</span><br><span class="line">            <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; conns.Length; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (conns[i] == <span class="literal">null</span>)</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                <span class="keyword">if</span> (!conns[i].isUse)</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                <span class="keyword">if</span>(conns[i].GetAdress()==SendIPAddress) &#123;</span><br><span class="line">                    index = i;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">//print(&quot; 将消息传播&quot; + conns[i].GetAdress());</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (index &lt; <span class="number">0</span>)</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            <span class="built_in">int</span> width = NetCloudServerManager.instance.width;</span><br><span class="line">            ProtocolCubeTexture protocolCube = <span class="keyword">new</span> ProtocolCubeTexture();</span><br><span class="line">            protocolCube.srcTexture = <span class="keyword">new</span> Texture2D(width, width);</span><br><span class="line">            <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++)</span><br><span class="line">                protocolCube.Coord[i] = <span class="built_in">int</span>.Parse(CoordStrs[i]);</span><br><span class="line">            <span class="keyword">for</span> (<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;<span class="number">6</span>;i++) &#123;</span><br><span class="line">                protocolCube.CubemapFace = i;</span><br><span class="line">                protocolCube.srcTexture.SetPixels(point.cubemap.GetPixels((CubemapFace)i));</span><br><span class="line">                protocolCube.srcTexture.Apply();</span><br><span class="line">                conns[index].SendMsg(protocolCube);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            NetCloudServerManager.instance.debug.text+= (<span class="string">&quot;\n&quot;</span> + protocolstr.str+<span class="string">&quot;渲染失败&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="PointMessage-点消息类"><a href="#PointMessage-点消息类" class="headerlink" title="PointMessage 点消息类"></a>PointMessage 点消息类</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">PointMessage</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> Vector3 pos;</span><br><span class="line">    <span class="keyword">public</span> Cubemap cubemap;</span><br><span class="line">    <span class="keyword">static</span> <span class="built_in">int</span> width;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PointMessage</span>()</span> &#123;</span><br><span class="line">        pos = Vector3.zero;</span><br><span class="line">        width = NetCloudServerManager.instance.width;</span><br><span class="line"></span><br><span class="line">        cubemap = <span class="keyword">new</span> Cubemap(width, TextureFormat.RGBA32, <span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="客户端"><a href="#客户端" class="headerlink" title="客户端"></a>客户端</h2><h3 id="Connect-类"><a href="#Connect-类" class="headerlink" title="Connect 类"></a>Connect 类</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">Connect</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Start is called before the first frame update</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Socket socket;</span><br><span class="line">    <span class="built_in">int</span> BUFFER_SIZE = NetCloudClientManager.instance.BUFFER_SIZE;</span><br><span class="line">    <span class="built_in">byte</span>[] readBuff ;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">int</span> buffCount = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Int32 msgLength = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">byte</span>[] lenByte = <span class="keyword">new</span> <span class="built_in">byte</span>[<span class="keyword">sizeof</span>(Int32)];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> ProtocolByte assistProtolByte;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> MsgHandle msgHandle = <span class="keyword">new</span> MsgHandle();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">enum</span> Status &#123;</span><br><span class="line">        None,Connect</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">public</span> Status status = Status.None;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="built_in">int</span> width = <span class="number">256</span>;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">public</span>  <span class="title">Connect</span>()</span> &#123;</span><br><span class="line">        readBuff = <span class="keyword">new</span> <span class="built_in">byte</span>[BUFFER_SIZE];</span><br><span class="line">        assistProtolByte = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">bool</span> <span class="title">Connetion</span>(<span class="params"><span class="built_in">string</span> host, <span class="built_in">int</span> port</span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//清理text</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//Socket</span></span><br><span class="line">            socket = <span class="keyword">new</span> Socket(AddressFamily.InterNetwork,</span><br><span class="line">            SocketType.Stream, ProtocolType.Tcp);</span><br><span class="line">            <span class="keyword">if</span> (host.Length == <span class="number">0</span>) &#123;</span><br><span class="line">                host = <span class="string">&quot;192.168.50.142&quot;</span>;</span><br><span class="line">                port = <span class="number">1234</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            socket.Connect(host, port);</span><br><span class="line">            status = Status.Connect;</span><br><span class="line">            NetCloudClientManager.instance.debug.text += (<span class="string">&quot;\n&quot;</span> + <span class="string">&quot;客户端地址&quot;</span> + socket.LocalEndPoint.ToString());</span><br><span class="line">            socket.BeginReceive(readBuff, buffCount, BUFFER_SIZE - buffCount, SocketFlags.None, ReceiveCb, readBuff);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span>(Exception e) &#123;</span><br><span class="line">            NetCloudClientManager.instance.debug.text += (<span class="string">&quot;\n&quot;</span> + <span class="string">&quot;客户端失败&quot;</span>+e.Message );</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// clientText.text = &quot;客户端地址&quot; + socket.LocalEndPoint.ToString();</span></span><br><span class="line">        <span class="comment">//Recv</span></span><br><span class="line"></span><br><span class="line">       </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">ReceiveCb</span>(<span class="params">IAsyncResult ar</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//count是接收数据的大小</span></span><br><span class="line">            <span class="built_in">int</span> count = socket.EndReceive(ar);</span><br><span class="line">            buffCount += count;</span><br><span class="line">            ProcessData();</span><br><span class="line">            socket.BeginReceive(readBuff,buffCount , BUFFER_SIZE-buffCount, SocketFlags.None, ReceiveCb, readBuff);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">           <span class="comment">// recvText.text += &quot;连接已断开&quot;;</span></span><br><span class="line">            socket.Close();</span><br><span class="line">            status = Status.None;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">ProcessData</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (buffCount &lt; <span class="keyword">sizeof</span>(Int32))</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        Array.Copy(readBuff, lenByte, <span class="keyword">sizeof</span>(Int32));</span><br><span class="line">        msgLength = BitConverter.ToInt32(lenByte, <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">if</span> (buffCount &lt; msgLength + <span class="keyword">sizeof</span>(Int32))</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        ProtocolBase protocol = assistProtolByte.Decode(readBuff, <span class="keyword">sizeof</span>(Int32), msgLength);</span><br><span class="line">        <span class="keyword">lock</span>(msgHandle.msgList) &#123;</span><br><span class="line">            msgHandle.msgList.Add(protocol);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">int</span> count = buffCount - msgLength - <span class="keyword">sizeof</span>(Int32);</span><br><span class="line">        Array.Copy(readBuff, <span class="keyword">sizeof</span>(Int32) + msgLength, readBuff, <span class="number">0</span>, count);</span><br><span class="line">        buffCount = count;</span><br><span class="line">        <span class="keyword">if</span> (buffCount &gt; <span class="number">0</span>)</span><br><span class="line">            ProcessData();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">bool</span> <span class="title">Close</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            socket.Close();</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            Debug.Log(<span class="string">&quot;关闭失败:&quot;</span> + e.Message);</span><br><span class="line">             <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">bool</span>  <span class="title">SendMsg</span>(<span class="params"> ProtocolBase protol</span>)</span> &#123;    </span><br><span class="line">        <span class="built_in">string</span> proName = protol.GetName();  ;</span><br><span class="line">        <span class="keyword">return</span> SendMsg(protol, proName);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">bool</span> <span class="title">SendMsg</span>(<span class="params">ProtocolBase protol,<span class="built_in">string</span> protolName</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (status != Status.Connect) &#123;</span><br><span class="line">            Debug.Log(<span class="string">&quot;404 Not Found&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ProtocolByte protocolByte1 = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        protocolByte1.AddString(protolName);</span><br><span class="line">        <span class="built_in">byte</span>[] protolNameByte = protocolByte1.bytes;</span><br><span class="line">        <span class="built_in">byte</span>[] b = protol.Encode();</span><br><span class="line">        <span class="built_in">byte</span>[] len1 = BitConverter.GetBytes(protolNameByte.Length + b.Length);</span><br><span class="line">        <span class="built_in">byte</span>[] sendByte = len1.Concat(protolNameByte).Concat(b).ToArray();</span><br><span class="line">        socket.Send(sendByte);</span><br><span class="line">        Debug.Log(GetStr(sendByte));</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span>  <span class="built_in">string</span> <span class="title">GetStr</span>(<span class="params"><span class="built_in">byte</span>[] bytes</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">string</span> str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>) <span class="keyword">return</span> str;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; bytes.Length; i++) &#123;</span><br><span class="line">            <span class="built_in">int</span> b = (<span class="built_in">int</span>)bytes[i];</span><br><span class="line">            str += b.ToString() + <span class="string">&quot; &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">#<span class="keyword">region</span> 地址辅助方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">GetClientAddress</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> socket.LocalEndPoint.ToString();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">GetLocalIp</span>()</span> &#123;</span><br><span class="line">        <span class="comment"><span class="doctag">///</span>获取本地的IP地址</span></span><br><span class="line">        <span class="built_in">string</span> AddressIP = <span class="built_in">string</span>.Empty;</span><br><span class="line">        <span class="keyword">foreach</span> (IPAddress _IPAddress <span class="keyword">in</span> Dns.GetHostEntry(Dns.GetHostName()).AddressList) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (_IPAddress.AddressFamily.ToString() == <span class="string">&quot;InterNetwork&quot;</span>) &#123;</span><br><span class="line"></span><br><span class="line">                AddressIP = _IPAddress.ToString();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> AddressIP;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">#<span class="keyword">endregion</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="消息处理类-1"><a href="#消息处理类-1" class="headerlink" title="消息处理类"></a>消息处理类</h3><p>在处理消息队列时一定需要上lock</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">MsgHandle</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> num = NetCloudClientManager.instance.HandleMsgNum;</span><br><span class="line">    <span class="keyword">public</span> List&lt;ProtocolBase&gt; msgList = <span class="keyword">new</span> List&lt;ProtocolBase&gt;();</span><br><span class="line"></span><br><span class="line">    ProtocolByte assistProtocol;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Running</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">while</span>(msgList.Count&gt;<span class="number">0</span>&amp;&amp;num&gt;<span class="number">0</span>) &#123;</span><br><span class="line">            num--;</span><br><span class="line">            <span class="comment">//处理长度和将协议分开</span></span><br><span class="line">            assistProtocol = msgList[<span class="number">0</span>] <span class="keyword">as</span> ProtocolByte;</span><br><span class="line">            <span class="keyword">if</span>(assistProtocol==<span class="literal">null</span>) &#123;</span><br><span class="line">                Debug.Log(<span class="string">&quot;消息转化失败&quot;</span>);</span><br><span class="line">                <span class="keyword">lock</span> (msgList) &#123;</span><br><span class="line">                    msgList.RemoveAt(<span class="number">0</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">string</span> protocolName=assistProtocol.GetName();</span><br><span class="line">            <span class="built_in">int</span> protocolNameLen=System.Text.Encoding.UTF8.GetBytes(protocolName).Length + <span class="keyword">sizeof</span>(Int32);</span><br><span class="line">            <span class="keyword">switch</span> (protocolName) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;ProtocolCubeTexture&quot;</span>:</span><br><span class="line">                    ProtocolCubeTexture protocolCubeTexture = <span class="keyword">new</span> ProtocolCubeTexture();</span><br><span class="line">                    ProtocolCubeTexture aprotocol = protocolCubeTexture.Decode(assistProtocol.bytes, protocolNameLen, assistProtocol.bytes.Length) <span class="keyword">as</span> ProtocolCubeTexture;</span><br><span class="line">                    <span class="keyword">if</span> (aprotocol == <span class="literal">null</span>) <span class="keyword">break</span>;</span><br><span class="line">                    Vector3 pos = <span class="keyword">new</span> Vector3(aprotocol.Coord[<span class="number">0</span>], aprotocol.Coord[<span class="number">1</span>], aprotocol.Coord[<span class="number">2</span>]);</span><br><span class="line">                    PointMessage point = NetCloudClientManager.instance.PM.Find((PointMessage p) =&gt; p.pos.Equals(pos));</span><br><span class="line">                    <span class="keyword">if</span>(point==<span class="literal">null</span>) point = <span class="keyword">new</span> PointMessage();</span><br><span class="line">                    </span><br><span class="line">                    point.ResolveToPointMessage(aprotocol.Coord, aprotocol.CubemapFace, aprotocol.srcTexture);</span><br><span class="line">                    </span><br><span class="line">                    NetCloudClientManager.instance.PM.Add(point);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">case</span> <span class="string">&quot;Start&quot;</span>:</span><br><span class="line">                    ProtocolStr protocolStr = <span class="keyword">new</span> ProtocolStr();</span><br><span class="line">                    ProtocolStr protocolstr = protocolStr.Decode(assistProtocol.bytes, protocolNameLen, assistProtocol.bytes.Length) <span class="keyword">as</span> ProtocolStr;</span><br><span class="line">                    NetCloudClientManager.instance.debug.text += (<span class="string">&quot;\n&quot;</span> + protocolstr.str);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">lock</span>(msgList) &#123;</span><br><span class="line">                msgList.RemoveAt(<span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//ProtocolCubeTexture protocol = msgList[0] as ProtocolCubeTexture;</span></span><br><span class="line">        &#125;</span><br><span class="line">        num = NetCloudClientManager.instance.HandleMsgNum;</span><br><span class="line">    &#125;   </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="PointMessage-点消息类-1"><a href="#PointMessage-点消息类-1" class="headerlink" title="PointMessage 点消息类"></a>PointMessage 点消息类</h3><p>区别于服务端，需要把相应的字节流变成texture，并将其贴到点消息的CubeMap上</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> System.Linq;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">PointMessage</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> Vector3 pos;</span><br><span class="line">    <span class="keyword">public</span> Cubemap cubemap;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> <span class="built_in">int</span> width;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">bool</span>[] FaceFlags ;</span><br><span class="line">    <span class="built_in">int</span> size;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PointMessage</span>()</span> &#123;</span><br><span class="line">        pos = Vector3.zero;</span><br><span class="line">        width = NetCloudClientManager.instance.width;</span><br><span class="line">        size = <span class="number">0</span>;</span><br><span class="line">        FaceFlags = <span class="keyword">new</span> <span class="built_in">bool</span>[] &#123; <span class="literal">false</span>, <span class="literal">false</span>, <span class="literal">false</span>, <span class="literal">false</span>, <span class="literal">false</span>, <span class="literal">false</span> &#125;;</span><br><span class="line">        cubemap = <span class="keyword">new</span> Cubemap(width, TextureFormat.RGBA32, <span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ResolveToPointMessage</span>(<span class="params"><span class="built_in">byte</span>[] byt </span>)</span> &#123;</span><br><span class="line">        <span class="built_in">string</span> str = System.Text.Encoding.UTF8.GetString(byt);</span><br><span class="line">        <span class="built_in">string</span>[] strs = str.Split(<span class="string">&#x27; &#x27;</span>);</span><br><span class="line">        pos.x = <span class="built_in">float</span>.Parse(strs[<span class="number">0</span>]);</span><br><span class="line">        pos.y = <span class="built_in">float</span>.Parse(strs[<span class="number">1</span>]);</span><br><span class="line">        pos.z = <span class="built_in">float</span>.Parse(strs[<span class="number">2</span>]);</span><br><span class="line">        <span class="built_in">string</span> CF = strs[<span class="number">3</span>]; </span><br><span class="line">        <span class="built_in">int</span> bytCount = System.Text.Encoding.UTF8.GetBytes(pos.x.ToString() + <span class="string">&quot; &quot;</span> + pos.y.ToString() + <span class="string">&quot; &quot;</span> + pos.z.ToString() + <span class="string">&quot; &quot;</span> + CF + <span class="string">&quot; &quot;</span>).Length;</span><br><span class="line">        <span class="built_in">byte</span>[] image = byt.Skip(bytCount).ToArray();</span><br><span class="line">        <span class="keyword">switch</span> (CF) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;PX&quot;</span>: cubemap.SetPixels(ColorToCubemap(image), CubemapFace.PositiveX); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;NX&quot;</span>: cubemap.SetPixels(ColorToCubemap(image), CubemapFace.NegativeX); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;PY&quot;</span>: cubemap.SetPixels(ColorToCubemap(image), CubemapFace.PositiveY); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;NY&quot;</span>: cubemap.SetPixels(ColorToCubemap(image), CubemapFace.NegativeY); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;PZ&quot;</span>: cubemap.SetPixels(ColorToCubemap(image), CubemapFace.PositiveZ); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;NZ&quot;</span>: cubemap.SetPixels(ColorToCubemap(image), CubemapFace.NegativeZ); <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        cubemap.Apply();</span><br><span class="line">        size++;</span><br><span class="line">        <span class="comment">//Texture2DClient.LoadImage(image);</span></span><br><span class="line">        <span class="comment">//Texture2DClient.Apply();</span></span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ResolveToPointMessage</span>(<span class="params"><span class="built_in">int</span>[] Coord,<span class="built_in">int</span> CubeMapFace,Texture2D texture</span>)</span> &#123;</span><br><span class="line">        pos.x = Coord[<span class="number">0</span>];</span><br><span class="line">        pos.y = Coord[<span class="number">1</span>];</span><br><span class="line">        pos.z = Coord[<span class="number">2</span>];</span><br><span class="line">        <span class="keyword">switch</span> (CubeMapFace) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">0</span>: cubemap.SetPixels( texture.GetPixels(), CubemapFace.PositiveX); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">1</span>: cubemap.SetPixels( texture.GetPixels(), CubemapFace.NegativeX); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">2</span>: cubemap.SetPixels( texture.GetPixels(), CubemapFace.PositiveY); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">3</span>: cubemap.SetPixels( texture.GetPixels(), CubemapFace.NegativeY); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">4</span>: cubemap.SetPixels( texture.GetPixels(), CubemapFace.PositiveZ); <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">5</span>: cubemap.SetPixels( texture.GetPixels(), CubemapFace.NegativeZ); <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Debug.Log(<span class="string">&quot;cubeface &quot;</span> + CubeMapFace);</span><br><span class="line">        </span><br><span class="line">       </span><br><span class="line">        FaceFlags[CubeMapFace] = <span class="literal">true</span>;</span><br><span class="line">        cubemap.Apply();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">Color[] <span class="title">ColorToCubemap</span>(<span class="params"><span class="built_in">byte</span>[] byt</span>)</span> &#123;</span><br><span class="line">        Texture2D textureTest = <span class="keyword">new</span> Texture2D(width, width);</span><br><span class="line">        textureTest.LoadImage(byt);</span><br><span class="line">        textureTest.Apply();</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> textureTest.GetPixels() ;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="built_in">int</span>  <span class="title">GetSize</span>()</span> &#123;</span><br><span class="line">        size = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="built_in">int</span> i=<span class="number">0</span>; i&lt;FaceFlags.Length;i++) &#123; <span class="keyword">if</span> (FaceFlags[i]) size++; &#125;</span><br><span class="line">        <span class="keyword">return</span> size;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">aa</span>()</span> &#123;</span><br><span class="line">        Debug.Log(<span class="string">&quot;aaaaa&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="协议"><a href="#协议" class="headerlink" title="协议"></a>协议</h2><p>个人理解：字节流协议和字符串流协议算是中间层协议，其他协议类可以聚合两个中间层协议进行编写。</p>
<h3 id="协议基类"><a href="#协议基类" class="headerlink" title="协议基类"></a>协议基类</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">ProtocolBase</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//解码器</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> ProtocolBase <span class="title">Decode</span>(<span class="params"><span class="built_in">byte</span>[] readBuff,<span class="built_in">int</span> start,<span class="built_in">int</span> length</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ProtocolBase();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//编码器</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="built_in">byte</span>[] <span class="title">Encode</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">byte</span>[] &#123; &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">GetName</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Base&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">virtual</span> <span class="built_in">string</span> <span class="title">GetDesc</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">    &#125;   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="字节流协议"><a href="#字节流协议" class="headerlink" title="字节流协议"></a>字节流协议</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> System.Linq;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">ProtocolByte</span> : <span class="title">ProtocolBase</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">byte</span>[] bytes;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> ProtocolBase <span class="title">Decode</span>(<span class="params"><span class="built_in">byte</span>[] readBuff, <span class="built_in">int</span> start, <span class="built_in">int</span> length</span>)</span> &#123;</span><br><span class="line">        ProtocolByte proByte = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        proByte.bytes = <span class="keyword">new</span> <span class="built_in">byte</span>[length];</span><br><span class="line">        Array.Copy(readBuff, start, proByte.bytes, <span class="number">0</span>, length);</span><br><span class="line">        <span class="keyword">return</span> (ProtocolBase)proByte;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">byte</span>[] <span class="title">Encode</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> bytes;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">string</span> <span class="title">GetName</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> GetString(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">string</span> <span class="title">GetDesc</span>()</span> &#123;</span><br><span class="line">        <span class="built_in">string</span> str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>) <span class="keyword">return</span> str;</span><br><span class="line">        <span class="keyword">for</span>(<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;bytes.Length;i++) &#123;</span><br><span class="line">            <span class="built_in">int</span> b = (<span class="built_in">int</span>)bytes[i];</span><br><span class="line">            str += b.ToString() + <span class="string">&quot; &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">#<span class="keyword">region</span> 字节辅助流</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">AddString</span>(<span class="params"><span class="built_in">string</span> str</span>)</span> &#123;</span><br><span class="line">        Int32 len = str.Length;</span><br><span class="line">        <span class="built_in">byte</span>[] lenBytes = BitConverter.GetBytes(len);</span><br><span class="line">        <span class="built_in">byte</span>[] strBytes = System.Text.Encoding.UTF8.GetBytes(str);</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>)</span><br><span class="line">            bytes = lenBytes.Concat(strBytes).ToArray();</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            bytes = bytes.Concat(lenBytes).Concat(strBytes).ToArray();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">GetString</span>(<span class="params"><span class="built_in">int</span> start, <span class="keyword">ref</span> <span class="built_in">int</span> end</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">if</span> (bytes.Length &lt; start + <span class="keyword">sizeof</span>(Int32))</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">        Int32 strLen = BitConverter.ToInt32(bytes, start);</span><br><span class="line">        <span class="keyword">if</span> (bytes.Length &lt; start + <span class="keyword">sizeof</span>(Int32) + strLen)</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="built_in">string</span> str = System.Text.Encoding.UTF8.GetString(bytes, start + <span class="keyword">sizeof</span>(Int32) , strLen);</span><br><span class="line">        end = start + <span class="keyword">sizeof</span>(Int32) + strLen;</span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">GetString</span>(<span class="params"><span class="built_in">int</span> start</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">int</span> end = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> GetString(start,<span class="keyword">ref</span> end);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">AddInt</span>(<span class="params"><span class="built_in">int</span> num</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">byte</span>[] numBytes = BitConverter.GetBytes(num);</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>)</span><br><span class="line">            bytes = numBytes;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            bytes = bytes.Concat(numBytes).ToArray();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">int</span> <span class="title">GetInt</span>(<span class="params"><span class="built_in">int</span> start,<span class="keyword">ref</span> <span class="built_in">int</span> end</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (bytes.Length &lt; start + <span class="keyword">sizeof</span>(Int32))</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        end = start + <span class="keyword">sizeof</span>(Int32);</span><br><span class="line">        <span class="keyword">return</span> BitConverter.ToInt32(bytes, start);</span><br><span class="line">    &#125;<span class="function"><span class="keyword">public</span> <span class="built_in">int</span> <span class="title">GetInt</span>(<span class="params"><span class="built_in">int</span> start</span>)</span> &#123;</span><br><span class="line">       </span><br><span class="line">        <span class="built_in">int</span> end = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> GetInt(start,<span class="keyword">ref</span> end);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">AddFloat</span>(<span class="params"><span class="built_in">float</span> num</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">byte</span>[] numBytes = BitConverter.GetBytes(num);</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>)</span><br><span class="line">            bytes = numBytes;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            bytes = bytes.Concat(numBytes).ToArray();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">float</span> <span class="title">GetFloat</span>(<span class="params"><span class="built_in">int</span> start, <span class="keyword">ref</span> <span class="built_in">int</span> end</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (bytes == <span class="literal">null</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (bytes.Length &lt; start + <span class="keyword">sizeof</span>(<span class="built_in">float</span>))</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        end = start + <span class="keyword">sizeof</span>(<span class="built_in">float</span>);</span><br><span class="line">        <span class="keyword">return</span> BitConverter.ToSingle(bytes, start);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">float</span> <span class="title">GetFloat</span>(<span class="params"><span class="built_in">int</span> start</span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">int</span> end = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> GetFloat(start, <span class="keyword">ref</span> end);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">#<span class="keyword">endregion</span></span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="字符串协议"><a href="#字符串协议" class="headerlink" title="字符串协议"></a>字符串协议</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">ProtocolStr</span> : <span class="title">ProtocolBase</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">string</span> str;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> ProtocolBase <span class="title">Decode</span>(<span class="params"><span class="built_in">byte</span>[] readBuff, <span class="built_in">int</span> start, <span class="built_in">int</span> length</span>)</span> &#123;</span><br><span class="line">        ProtocolStr proStr = <span class="keyword">new</span> ProtocolStr();    </span><br><span class="line">        proStr.str = System.Text.Encoding.UTF8.GetString(readBuff, start, length);</span><br><span class="line">        <span class="keyword">return</span> (ProtocolBase) proStr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">byte</span>[] <span class="title">Encode</span>()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> System.Text.Encoding.UTF8.GetBytes(str);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">string</span> <span class="title">GetName</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (str.Length == <span class="number">0</span>) <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">return</span> str.Split(<span class="string">&#x27;,&#x27;</span>)[<span class="number">0</span>];  </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">string</span> <span class="title">GetDesc</span>()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="ProtocolCubeTexture-协议"><a href="#ProtocolCubeTexture-协议" class="headerlink" title="ProtocolCubeTexture 协议"></a>ProtocolCubeTexture 协议</h3><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> System.Linq;</span><br><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">ProtocolCubeTexture</span> : <span class="title">ProtocolBase</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> width = NetCloudClientManager.instance.width;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span>[] Coord = <span class="keyword">new</span> <span class="built_in">int</span>[] &#123; <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span> &#125;;</span><br><span class="line">    <span class="keyword">public</span> Texture2D srcTexture;</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> CubemapFace = <span class="number">-1</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//解码器</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> ProtocolBase <span class="title">Decode</span>(<span class="params"><span class="built_in">byte</span>[] readBuff, <span class="built_in">int</span> start, <span class="built_in">int</span> length</span>)</span> &#123;</span><br><span class="line">        ProtocolCubeTexture proTexture = <span class="keyword">new</span> ProtocolCubeTexture();</span><br><span class="line">        ProtocolByte proByte = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        proByte.bytes = readBuff;</span><br><span class="line">        <span class="keyword">for</span>(<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++) &#123;</span><br><span class="line">            proTexture.Coord[i] = proByte.GetInt(start, <span class="keyword">ref</span> start);</span><br><span class="line">        &#125;</span><br><span class="line">        proTexture.CubemapFace = proByte.GetInt(start, <span class="keyword">ref</span> start);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//图片解析</span></span><br><span class="line">        <span class="built_in">int</span> srcStart = start;</span><br><span class="line">        <span class="built_in">int</span> srcLength = readBuff.Length-start;</span><br><span class="line">        <span class="built_in">byte</span>[] srcbyte = <span class="keyword">new</span> <span class="built_in">byte</span>[srcLength];</span><br><span class="line">        Array.Copy(readBuff, srcStart, srcbyte, <span class="number">0</span>, srcLength);</span><br><span class="line">        proTexture.srcTexture = <span class="keyword">new</span> Texture2D(width,width);</span><br><span class="line">        proTexture.srcTexture.LoadImage(srcbyte);</span><br><span class="line">        proTexture.srcTexture.Apply();</span><br><span class="line">        <span class="comment">//NetCloudClientManager.instance.tex[proTexture.CubemapFace] = proTexture.srcTexture;</span></span><br><span class="line">        <span class="keyword">return</span> (ProtocolBase) proTexture;</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    <span class="comment">//编码器</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">byte</span>[] <span class="title">Encode</span>()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//立方体面编译</span></span><br><span class="line">        </span><br><span class="line">        ProtocolByte proByte = <span class="keyword">new</span> ProtocolByte();</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">for</span>(<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++) &#123;</span><br><span class="line">            proByte.AddInt(Coord[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        proByte.AddInt(CubemapFace);</span><br><span class="line">        <span class="built_in">byte</span>[] cubeFaceBytes = proByte.Encode();</span><br><span class="line">        <span class="comment">//图片编译</span></span><br><span class="line">        <span class="keyword">if</span> (srcTexture == <span class="literal">null</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="built_in">byte</span>[] srcTextureByte = srcTexture.EncodeToJPG();</span><br><span class="line">        <span class="comment">//byte[] lenByte = BitConverter.GetBytes(srcTextureByte.Length + cubeFaceBytes.Length);</span></span><br><span class="line">        <span class="built_in">byte</span>[] byt = cubeFaceBytes.Concat(srcTextureByte).ToArray();</span><br><span class="line">        <span class="keyword">return</span> byt;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">string</span> <span class="title">GetName</span>()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;ProtocolCubeTexture&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">override</span> <span class="built_in">string</span> <span class="title">GetDesc</span>()</span> &#123;</span><br><span class="line">        <span class="built_in">string</span> str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="built_in">byte</span>[] srcTextureByte = srcTexture.EncodeToJPG();</span><br><span class="line">        <span class="keyword">if</span> (srcTextureByte == <span class="literal">null</span>) <span class="keyword">return</span> str;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; srcTextureByte.Length; i++) &#123;</span><br><span class="line">            <span class="built_in">int</span> b = (<span class="built_in">int</span>)srcTextureByte[i];</span><br><span class="line">            str += b.ToString() + <span class="string">&quot; &quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="/music/jucilang.mp3">
            </audio>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='99f95a936c6fcdb1bd67'
        data-cs='c2d1bba3d6b7f9ade4f3de09a63925bd837c25ea'
        data-r='mrchenlearnspace.github.io'
        data-o='MrChenLearnSpace'
        data-a='MrChenLearnSpace'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%A8%E6%99%AF%E5%9B%BE"><span class="toc-number">1.</span> <span class="toc-text">全景图</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E5%82%A8%E5%A4%87"><span class="toc-number">1.1.</span> <span class="toc-text">知识储备</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%86%E5%85%A8%E6%99%AF%E5%9B%BE%E8%BD%AC%E5%8F%98%E4%B8%BAByte%E6%95%B0%E7%BB%84"><span class="toc-number">1.1.1.</span> <span class="toc-text">将全景图转变为Byte数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%86Byte%E6%95%B0%E7%BB%84%E5%8F%98%E6%88%90%E5%85%A8%E6%99%AF%E5%9B%BE"><span class="toc-number">1.1.2.</span> <span class="toc-text">将Byte数组变成全景图</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E4%BC%A0%E8%BE%93"><span class="toc-number">1.1.3.</span> <span class="toc-text">网络传输</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86"><span class="toc-number">1.1.4.</span> <span class="toc-text">图像处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%8F%E8%AE%AE%E5%A4%84%E7%90%86"><span class="toc-number">1.1.5.</span> <span class="toc-text">协议处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%9C%AC%E5%9C%B0-I-P-%E5%9C%B0%E5%9D%80"><span class="toc-number">1.1.6.</span> <span class="toc-text">获取本地 I P 地址</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E7%BD%91%E7%BB%9C%E6%9E%B6%E6%9E%84"><span class="toc-number">1.2.</span> <span class="toc-text">基本网络架构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF"><span class="toc-number">1.3.</span> <span class="toc-text">服务器端</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Connection"><span class="toc-number">1.3.1.</span> <span class="toc-text">Connection</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Accept-Cb-%E5%BC%82%E6%AD%A5%E7%9B%91%E5%90%AC"><span class="toc-number">1.3.2.</span> <span class="toc-text">Accept Cb 异步监听</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Recive-Cb%E5%BC%82%E6%AD%A5%E6%8E%A5%E6%94%B6"><span class="toc-number">1.3.3.</span> <span class="toc-text">Recive Cb异步接收</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Conn%E7%B1%BB"><span class="toc-number">1.3.4.</span> <span class="toc-text">Conn类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E5%A4%84%E7%90%86%E7%B1%BB"><span class="toc-number">1.3.5.</span> <span class="toc-text">消息处理类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#PointMessage-%E7%82%B9%E6%B6%88%E6%81%AF%E7%B1%BB"><span class="toc-number">1.3.6.</span> <span class="toc-text">PointMessage 点消息类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF"><span class="toc-number">1.4.</span> <span class="toc-text">客户端</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Connect-%E7%B1%BB"><span class="toc-number">1.4.1.</span> <span class="toc-text">Connect 类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E5%A4%84%E7%90%86%E7%B1%BB-1"><span class="toc-number">1.4.2.</span> <span class="toc-text">消息处理类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#PointMessage-%E7%82%B9%E6%B6%88%E6%81%AF%E7%B1%BB-1"><span class="toc-number">1.4.3.</span> <span class="toc-text">PointMessage 点消息类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.5.</span> <span class="toc-text">协议</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%8F%E8%AE%AE%E5%9F%BA%E7%B1%BB"><span class="toc-number">1.5.1.</span> <span class="toc-text">协议基类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.5.2.</span> <span class="toc-text">字节流协议</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.5.3.</span> <span class="toc-text">字符串协议</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ProtocolCubeTexture-%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.5.4.</span> <span class="toc-text">ProtocolCubeTexture 协议</span></a></li></ol></li></ol></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/blog/js/plugin.js"></script>
<script src="/blog/js/typed.js"></script>
<script src="/blog/js/diaspora.js"></script>


<link rel="stylesheet" href="/blog/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/blog/photoswipe/default-skin/default-skin.css">


<script src="/blog/photoswipe/photoswipe.min.js"></script>
<script src="/blog/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>



<!-- Google Analytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YMJ8CBH8F7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-YMJ8CBH8F7');
</script>
<!-- End Google Analytics -->


</html>
